﻿using ESMAM.BLL.SYS;
using ESMAM.Web.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MJTop.Common;
using System.Drawing;
using ESMAM.Tools;

namespace ESMAM.WebApp.Controllers
{
    public class LoginController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Index(string loginName, string password,string checkcode)
        {
            var cc = new CaptchaControl();

            if (cc.CaptchaIsExpires())
            {
                return View(Retful.CreateFail("验证码已失效！"));
            }

            if (!cc.CaptchaValidate(checkcode))
            {
                return View(Retful.CreateFail("验证码输入不正确！"));
            }

            var retful = UserBLL.Login(loginName, password);
            if (retful.Code == 1)
            {
                Session[BaseController.CONST_SESSION_USER] = retful.Value;
                CookieHelper.SetCookie(BaseController.CONST_COOKIE_LANG, retful.Value.shiyyy+"", DateTime.Now.AddDays(15));                
                return Redirect("/Home/Index");
            }
            else
            {
                return View(retful);
            }
        }

        [HttpGet]
        public void checkcode()
        {
            var config = new CaptchaControl();
            //背景是否动态
            config.IsBackgroundDynamic = false;
            //验证码是否动态
            config.IsForegroundDynamic = false;
            config.Height = 34;
            config.Width = 110;
            //设置字体
            //config.Fonts = new List<CaptchaFontArrayItem>()
            //{
            //    new CaptchaFontArrayItem(new Font("Broadway",1)),
            //    new CaptchaFontArrayItem(new Font("Cooper Black",1))
            //};
            //背景颜色
            config.BackColor = Color.Bisque;

            config.ExpireTime = 20 * 60;

            var h = new Captcha();
            h.ProcessRequest();
        }

        public ActionResult LogOut()
        {
            Session.Clear();
            Session.Abandon();
            foreach (var key in Request.Cookies.AllKeys)
            {
                if (key == BaseController.CONST_COOKIE_UNAME || key == BaseController.CONST_COOKIE_LANG)
                {
                    continue;
                }
                Response.Cookies[key].Expires = DateTime.Now.AddDays(-30);
            }
            return Redirect("/Login/Index");
        }

        public ActionResult login_timeout()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ajaxlogin(string loginName, string password)
        {
            var retful = UserBLL.Login(loginName, password);
            if (retful.Code == 1)
            {
                Session[BaseController.CONST_SESSION_USER] = retful.Value;
                return this.JsonBJUI(200, "登陆成功！");
            }
            else
            {
                return this.JsonBJUI(300, retful.Msg);
            }
        }

    }
}